# python -m pip install pyserial

import serial  # 导入串口包
import time  # 导入时间包

# 对应耀华电子秤协议

ser = serial.Serial("COM6", 9600, timeout=1)
ser.flushInput()  # 清空缓冲区


def main():
    while True:
        count = ser.inWaiting()
        if count != 0:
            recv = ser.read(ser.in_waiting).decode("utf-8")
            checktime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

            splitres = (recv.splitlines())

            if len(splitres) != 2:
                continue
            if splitres[0].strip() != splitres[1].strip():
                continue
            if splitres[0].strip() == '' or splitres[1].strip() == '':
                continue

            #print(splitres[0].strip())

            print(checktime, " ---  recv --> ", yaohuadeal(splitres[0].strip()))

        time.sleep(0.1)  #


def yaohuadeal(inyaohuaread):
    yaohuares = str(inyaohuaread[2:9])+'kg'
    return yaohuares


if __name__ == '__main__':
    main()
